package com.animee.app4.bean;

import com.bumptech.glide.Glide;
import ohos.agp.components.*;
import ohos.app.Context;
import com.animee.app4.ResourceTable;

import java.util.List;

public class NewsListProvider extends BaseItemProvider {
    Context context;
    List<NewsBean.ResultDTO.DataDTO> mDatas;

    public NewsListProvider(Context context, List<NewsBean.ResultDTO.DataDTO> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            component = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_item_news,componentContainer,false);
        }
        Text titleTv = component.findComponentById(ResourceTable.Id_item_tv_title);
        Text authorTv = component.findComponentById(ResourceTable.Id_item_tv_author);
        Text timeTv = component.findComponentById(ResourceTable.Id_item_tv_time);
        Image img1 = component.findComponentById(ResourceTable.Id_item_img1);
        Image img2 = component.findComponentById(ResourceTable.Id_item_img2);
        Image img3 = component.findComponentById(ResourceTable.Id_item_img3);

        NewsBean.ResultDTO.DataDTO dto = mDatas.get(i);
        titleTv.setText(dto.getTitle());
        authorTv.setText(dto.getAuthor_name());
        timeTv.setText(dto.getDate());

        String imgurl1 = dto.getThumbnail_pic_s();
        String imgurl2 = dto.getThumbnail_pic_s02();
        String imgurl3 = dto.getThumbnail_pic_s03();

        showImage(img1, imgurl1);
        showImage(img2,imgurl2);
        showImage(img3,imgurl3);
        return component;
    }

    private void showImage(Image img1, String imgurl1) {
        if (imgurl1 ==null || img1.equals("")){
            img1.setVisibility(Component.HIDE);
        }else {
            img1.setVisibility(Component.VISIBLE);
            Glide.with(context).load(imgurl1)
                    .placeholder(ResourceTable.Media_load_failed)
                    .error(ResourceTable.Media_load_failed)
                    .into(img1);
        }
    }
}